package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TrainingRecordMapper;
import com.ruoyi.business.domain.TrainingRecord;
import com.ruoyi.business.service.ITrainingRecordService;

import javax.validation.Validator;

/**
 * 培训授课记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class TrainingRecordServiceImpl  extends BaseServiceImpl implements ITrainingRecordService
{
    @Autowired
    private TrainingRecordMapper trainingRecordMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询培训授课记录
     * 
     * @param id 培训授课记录主键
     * @return 培训授课记录
     */
    @Override
    public TrainingRecord selectTrainingRecordById(Long id)
    {
        return trainingRecordMapper.selectTrainingRecordById(id);
    }

    /**
     * 查询培训授课记录列表
     * 
     * @param trainingRecord 培训授课记录
     * @return 培训授课记录
     */
    @Override
    public List<TrainingRecord> selectTrainingRecordList(TrainingRecord trainingRecord)
    {
        return trainingRecordMapper.selectTrainingRecordList(trainingRecord);
    }

    /**
     * 新增培训授课记录
     * 
     * @param trainingRecord 培训授课记录
     * @return 结果
     */
    @Override
    public int insertTrainingRecord(TrainingRecord trainingRecord)
    {
        trainingRecord.setCreateTime(DateUtils.getNowDate());
        return trainingRecordMapper.insertTrainingRecord(trainingRecord);
    }

    /**
     * 修改培训授课记录
     * 
     * @param trainingRecord 培训授课记录
     * @return 结果
     */
    @Override
    public int updateTrainingRecord(TrainingRecord trainingRecord)
    {
        trainingRecord.setUpdateTime(DateUtils.getNowDate());
        return trainingRecordMapper.updateTrainingRecord(trainingRecord);
    }

    /**
     * 批量删除培训授课记录
     * 
     * @param ids 需要删除的培训授课记录主键
     * @return 结果
     */
    @Override
    public int deleteTrainingRecordByIds(Long[] ids)
    {
        return trainingRecordMapper.deleteTrainingRecordByIds(ids);
    }

    /**
     * 删除培训授课记录信息
     * 
     * @param id 培训授课记录主键
     * @return 结果
     */
    @Override
    public int deleteTrainingRecordById(Long id)
    {
        return trainingRecordMapper.deleteTrainingRecordById(id);
    }

    @Override
    public String importData(List<TrainingRecord> recordList, boolean updateSupport, String operName) {
    if (recordList == null || recordList.isEmpty()) {
        throw new ServiceException("导入培训记录数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
    for (TrainingRecord record : recordList) {
        try {
            i++;
            if (record.getAwardYear() == null || record.getAwardYear().equals("") || record.getEmployeeId() ==null || record.getEmployeeId().equals("") || !isAwardYearValid(record.getAwardYear())) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }

            // 验证是否存在这个培训记录
            TrainingRecord existingRecord = trainingRecordMapper.selectTrainingRecordById(record.getId());
            if (existingRecord == null) {
                // 验证数据
                BeanValidators.validateWithException(validator, record);
                // 设置初始值
                record.setCreateTime(DateUtils.getNowDate());
                record.setCreateBy(operName);
                trainingRecordMapper.insertTrainingRecord(record);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、培训记录 "  + " 导入成功");
            } else if (updateSupport) {
                // 验证数据
                BeanValidators.validateWithException(validator, record);
                record.setId(existingRecord.getId());
                record.setUpdateTime(DateUtils.getNowDate());
                record.setUpdateBy(operName);
                trainingRecordMapper.updateTrainingRecord(record);
                successNum++;
                //successMsg.append("<br/>" + successNum + "、培训记录 "  + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、培训记录 "  + " 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、培训记录 " + " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }

        setCacheInfo("培训授课记录信息",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
